package edu.sctu.yueding.adapter;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.RadioButton;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;


import edu.sctu.yueding.R;
import edu.sctu.yueding.bean.VoteBean;
import edu.sctu.yueding.bean.VoteItemBean;
import edu.sctu.yueding.config.CacheConfig;
import edu.sctu.yueding.utils.httputils.SharePreference;

/**
 * Created by zhengsenwen on 2016/8/19.
 */
public class VoteAdapter extends BaseAdapter {

    private Activity activity;
    private List<VoteBean.Items> list;
    public HashMap<String, Boolean> states = new HashMap<String, Boolean>();//用于记录每个RadioButton的状态，并保证只可选一个


    private boolean flag = false;

    public VoteAdapter(Activity activity,  List<VoteBean.Items> list) {
        this.activity = activity;
        this.list = list;
    }

    public VoteAdapter(Activity activity) {
        this.activity = activity;
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(activity).inflate(R.layout.item_vote_listview, null);

        final RadioButton radioButton = (RadioButton) view.findViewById(R.id.item_vote_listView_radio_button);
        TextView selection = (TextView) view.findViewById(R.id.item_vote_listView_selection);
        selection.setText(list.get(position).getItem());


        /**
         * 当RadioButton被选中时，将其状态记录进States中，并更新其他RadioButton的状态使它们不被选中
         */
        radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                for (String key : states.keySet()) {
                    states.put(key, false);
                }
                states.put(String.valueOf(position), radioButton.isChecked());

                VoteAdapter.this.notifyDataSetChanged();

            }
        });


        if (states.get(String.valueOf(position)) == null || states.get(String.valueOf(position)) == false) {
            flag = false;
            states.put(String.valueOf(position), false);
        } else {
            flag = true;
            radioButton.setChecked(flag);
            //根据选中状态设置图标
            radioButton.setBackgroundResource(R.drawable.vote_selected);
            SharePreference.getInstance(activity).setCache(CacheConfig.CACHE_VOTE_ITEM_ID, list.get(position).getId());
            SharePreference.getInstance(activity).setCache(CacheConfig.CACHE_VOTE_FLAG, flag);
        }

        return view;
    }

}
